package com.augurit.wf.bi.web;

import java.util.ArrayList;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.ExtResult;
import com.augurit.common.util.ExtUtils;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfPageElementService;
import com.augurit.wf.bi.web.form.WfPageElementForm;

@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "wf-page-element.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/wf/bi/page-elem/pub-page-elems-mgmt.jsp")
})
public class WfPageElementAction extends ExtCrudActionSupport<WfPageElementForm>{

	@Autowired
	private IWfPageElementService wfPageElementService;
	
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;

	private WfPageElementForm form;
	private Long id;
	private Long[] ids;
	private Page<WfPageElementForm> page = new Page<WfPageElementForm>(10);


	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfPageElementService.cascadeSearchBySql(page, form);
		ExtBaseAction.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 获取公共的页面元素
	 * @return
	 * @throws Exception
	 */
	public String getPublicPageElements()throws Exception  {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfPageElementService.getPublicElements(page, form);
		ExtBaseAction.extRenderGridJson(page);
		return null;
	}
	
	public String getPublicContainElements()throws Exception  {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfPageElementService.getPublicContainElements(page, form);
		ExtBaseAction.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 根据模板id获取页面元素
	 */
	public String getByTemplateId()throws Exception  {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfBusTemplateService.getPageElemByTemplateId(page, form.getTemplateId());
		if(page == null || page.getResult() == null || page.getResult().size() == 0){
			ExtResult extResult =new  ExtResult(true);
			extResult.setResult(new ArrayList<WfPageElementForm>());
			extResult.setTotalCount(0);
			ExtBaseAction.renderJson(extResult);
		}
		else
			ExtBaseAction.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 为业务流程模板获取公共页面元素
	 * @return
	 * @throws Exception
	 */
	public String getPublicElementForTemplate()throws Exception  {
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		wfBusTemplateService.getPublicElementNotInTemplate(form.getTemplateId(), page, form);
		ExtBaseAction.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addPublicElementsForTemplate()throws Exception{
		if(ids != null && ids.length > 0 && form.getTemplateId() != null){
			for(Long id: ids){
				WfPageElementForm elem = wfPageElementService.get(id);
				elem.setTemplateId(form.getTemplateId());
				wfPageElementService.addPageElement(elem);
			}
		}
		return null;
	}
	
	/**
	 * 删除公共的页面元素
	 * @return
	 * @throws Exception
	 */
	public String deletePublicElements() throws Exception{
		try{
			if(ids != null && ids.length > 0){
				wfPageElementService.deletePublicElements(ids);
			}
			ExtBaseAction.extRenderSuccess();
		}
		catch(Exception e){
			e.printStackTrace();
			ExtBaseAction.extRenderFailture();
		}
		return null;
	}
	

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		if(id != null){
			form = this.wfPageElementService.get(id);
		}
		ExtBaseAction.extRenderFormJson(form);
		return null;
	}

	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save() throws Exception {
		try{
			wfPageElementService.addPageElement(form);
			ExtBaseAction.extRenderSuccess();
		}
		catch(Exception e){
			e.printStackTrace();
			ExtBaseAction.extRenderFailture();
		}
		
		return null;
	}
	
	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		wfPageElementService.delete(ids);
		ExtBaseAction.extRenderSuccess();
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public String codeUniqueCheck() {
		if(wfPageElementService.isElemCodeExist(form.getElementCode(), form.getId())){
			renderJson("{\"exist\":true}");
		}
		else{
			renderJson("{\"exist\":false}");
		}
		return null;
	}
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = wfPageElementService.get(id);
		}
		else
			form = new WfPageElementForm();
	}
	

	public WfPageElementForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<WfPageElementForm> getPage() {
		return page;
	}


	public void setPage(Page<WfPageElementForm> page) {
		this.page = page;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}
	
}
